/**
 * Created by huachuan on 2016/1/14.
 */
angular.module('lbscontrollers',[])
  //发现的首页（约局）的Controller
  .controller('LbsCtrl',function($scope,$location,$state,$ionicScrollDelegate,LBS,Loading,Storage,History){

    $scope.search = {
      city_name:''
    };

    $scope.$on('$ionicView.beforeEnter',function(){
     var hotCityName = LBS.getLocationCityName();
      if(hotCityName!=null && hotCityName!=''){
        LBS.getHotCityByName(hotCityName);
      }
    })
    //离开时清空输入框
    $scope.$on('$ionicView.beforeLeave',function(){
      $scope.search = {
        city_name:''
      };
    })
    $scope.$on('LBS.getHotCityJsonUpdated',function(){
      $scope.localCity = LBS.getHotCityJson();
    });
    Loading.showLoading();
    LBS.getCityList();
    $scope.$on('LBS.getCityListUpdated',function(){
      $scope.cityList = LBS.getCityListJson();
      Loading.hideLoading();
    });
    //切换当前定位的城市
    $scope.selectCity = function(cityIndex){
      LBS.getCityById(cityIndex);

      //$state.transitionTo('tab.discoverIndex');
    }

    $scope.$on('LBS.getCityJsonUpdated',function(){
      var selectCity = LBS.getCityJson();
      Storage.set('lbsCity',selectCity);
      History.goback();
    });

    //跳转到对应字母的会员列表
    $scope.gotoList = function(id){
      $location.hash(id);
      $ionicScrollDelegate.anchorScroll(true);
    };

    $scope.location = function(){
      LBS.getPosition();
    }

    $scope.$on('LBS.locationSuccess',function(){
      var location = LBS.getLocation();
      var point = new BMap.Point(location.longitude,location.latitude);
      var gc = new BMap.Geocoder();
      gc.getLocation(point, function (rs) {
        var addComp = rs.addressComponents;
        var city  = addComp.city;
        LBS.getHotCityByName(city);
      });
    });
    $scope.$on('LBS.getCityJsonUpdated',function(){
      $scope.localCity = LBS.getCityJson();
      Storage.set("nowCity",$scope.localCity);
    })

      $scope.goback = function(){
        History.goback();
      }
  })

